package cn.gbase.jiangsu.data.transfer.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Streams;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.SupplierUser;
import cn.gbase.jiangsu.data.transfer.bean.TFile;
import cn.gbase.jiangsu.data.transfer.service.TFileService;
import cn.gbase.jiangsu.data.transfer.util.ServletUtils;
import cn.gbase.jiangsu.data.transfer.util.SystemContext;

/**
 * 文件上传管理的Action, 使用Restful风格的Urls:
 * 
 * List page     : GET /task/
 * Create page   : GET /task/create
 * Create action : POST /task/create
 * Update page   : GET /task/update/{id}
 * Update action : POST /task/update
 * Delete action : GET /task/delete/{id}
 * 
 */

@IocBean(args = { "refer:dao" })
@At("/wjsc")
public class TFileAction extends IdEntityService<TFile> {

	public TFileAction() {
		super();
	}

	public TFileAction(Dao dao) {
		super(dao);
	}

	@Inject
	private TFileService tFileService;

	private static final Log log = Logs.get();

	@POST
	@At("/create")
	@AdaptBy(type = UploadAdaptor.class)
	@Ok("jsp:/supplier/uploadPic")
	public Object create(@Param("uploadFile") TempFile tf, @Param("ywlx") String ywlx, @Param("ywid") String ywid,
			@Param("fromPage") String fromPage,
			@Attr(scope = Scope.SESSION, value = SystemContext.SUPPLIERUSER) SupplierUser supplierUser,
			HttpServletRequest req) {
		try {
			TFile file = new TFile();
			file.setWjmc(tf.getMeta().getFileLocalName());
			/*file.setScyhId(supplierUser.getId() + "");
			file.setScyh(supplierUser.getUsername());*/
			file.setScsj(new Timestamp(System.currentTimeMillis()));
			file.setYwid(ywid);
			tFileService.saveOrUpdate(file, tf.getFile(), tf.getMeta());

			List<TFile> fileList = this.dao().query(TFile.class, Cnd.where("ywid", "=", ywid));
			req.setAttribute("ywlx", ywlx);
			req.setAttribute("ywid", ywid);
			req.setAttribute("fromPage", fromPage);
			return fileList;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

	@At("/query/*")
	@Ok("jsp:/supplier/uploadPic")
	public Object query(String ywid, String fromPage, HttpServletRequest req) {
		List<TFile> fileList = tFileService.queryAllFile(ywid, req);
		req.setAttribute("ywid", ywid);
		req.setAttribute("fromPage", fromPage);
		return fileList;
	}

	@At("/delete/*")
	@Ok("jsp:/supplier/uploadPic")
	public Object delete(Integer wjId, @Param("ywid") String ywid, @Param("fromPage") String fromPage,
			HttpServletRequest req) {
		try {
			this.dao().delete(TFile.class, wjId);
			tFileService.deleteFile(wjId);
			List<TFile> fileList = this.dao().query(TFile.class, Cnd.where("ywid", "=", ywid));
			req.setAttribute("ywid", ywid);
			req.setAttribute("fromPage", fromPage);
			return fileList;
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param ids
	 * @return
	 */
	@At("/download/?")
	@Ok("void")
	public void download(Integer wjId, @Param("wjmc") String wjmc, HttpServletResponse response) throws IOException {
		File downloadFile = tFileService.getFile(wjId);
		ServletUtils.setFileDownloadHeader(response, wjmc);
		Streams.writeAndClose(response.getOutputStream(), Streams.fileIn(downloadFile));
	}

	//获取该商品下所有文件的别名
	@At("/getFileBmList/?")
	@Ok("json")
	public Object getFileBmList(String idNews) {
		return this.dao().query(TFile.class, Cnd.where("ywid", "=", idNews));
	}
}
